(function($){
    function crateCommentInfo(obj){

        if(typeof(obj.time) == "undefined" || obj.time == ""){
            obj.time = getNowDateFormat();
        }

        var el = "<div class='comment-info'><header><img src='"+obj.img+"'></header><div class='comment-right'><h3>"+obj.replyName+"</h3><input value='"+obj.commentUserId+","+obj.commentId+"' style='display:none;' ></input>"
            +"<div class='comment-content-header'><span><i class='glyphicon glyphicon-time'></i>"+obj.time+"</span>";

        if(typeof(obj.address) != "undefined" && obj.browse != ""){
            el =el+"<span><i class='glyphicon glyphicon-map-marker'></i>"+obj.address+"</span>";
        }
        el = el+"</div><p class='content' style='word-wrap: break-word; word-break: break-all;'>"+obj.content+"</p><div class='comment-content-footer'><div class='row'><div class='col-md-10'>";

        if(typeof(obj.osname) != "undefined" && obj.osname != ""){
            el =el+"<span><i class='glyphicon glyphicon-pushpin'></i> 来自:"+obj.osname+"</span>";
        }

        if(typeof(obj.browse) != "undefined" && obj.browse != ""){
            el = el + "<span><i class='glyphicon glyphicon-globe'></i> "+obj.browse+"</span>";
        }

        el = el + "</div><div class='col-md-2'><span class='reply-btn' style='display: none;'>回复</span></div></div></div><div class='reply-list'>";
        if(obj.replyBody != "" && obj.replyBody.length > 0){
            var arr = obj.replyBody;
            for(var j=0;j<arr.length;j++){
                var replyObj = arr[j];
                el = el+createReplyComment(replyObj);
            }
        }
        el = el+"</div></div></div>";
        return el;
    }

    //返回每个回复体内容
    function createReplyComment(reply){
        var replyEl = "<div class='reply'><div><a href='javascript:void(0)' class='replyname' data='"+reply.commentUserId+","+reply.parentCommentId+"'>"+reply.replyName+"</a>:<a href='javascript:void(0)'>@"+reply.beReplyName+"</a><span style='word-wrap: break-word;word-break: break-all;'>"+reply.content+"</span></div>"
            + "<p><span>"+reply.time+"</span> <span class='reply-list-btn'>回复</span></p></div>";
        return replyEl;
    }
    function getNowDateFormat(){
        var nowDate = new Date();
        var year = nowDate.getFullYear();
        var month = filterNum(nowDate.getMonth()+1);
        var day = filterNum(nowDate.getDate());
        var hours = filterNum(nowDate.getHours());
        var min = filterNum(nowDate.getMinutes());
        var seconds = filterNum(nowDate.getSeconds());
        return year+"-"+month+"-"+day+" "+hours+":"+min+":"+seconds;
    }
    function filterNum(num){
        if(num < 10){
            return "0"+num;
        }else{
            return num;
        }
    }
    function replyClick(el){
        var replyName = getLocalStorage('malongxiaozhan-user-userName',expUser);
        if(replyName===null || replyName==='undefined'){
            alert("未登录");
            return false;
        }

        el.parent().parent().append("<div class='replybox' ><textarea cols='80' rows='50' placeholder='来说几句吧......' class='mytextarea' ></textarea><span class='send'>发送</span></div>")
            .find(".send").click(function(){
            var content = $(this).prev().val();
            if(content !== "" || content !== null){
                var parentEl = $(this).parent().parent().parent().parent();
                var obj = new Object();
                obj.replyName="匿名";
                if(el.parent().parent().hasClass("reply")){
                    var replyFind = el.parent().parent().find("a:first");
                    obj.beReplyName = replyFind.text();
                    //回复数据
                    var commentValue = replyFind.attr("data").split(",");
                    obj.parentCommentUserId=commentValue[0];
                    obj.parentCommentId=commentValue[1];
                }else{
                    obj.beReplyName=parentEl.find("h3").text();
                    //回复数据
                    var commentValue = parentEl.find("input").val().split(",");
                    obj.parentCommentUserId=commentValue[0];
                    obj.parentCommentId=commentValue[1];
                }
                obj.commentContent=content;
                $(".replybox").remove();

                //保存回复
                var articleId=$(".commentContentInput").val();
                obj.articleId=articleId;
                comReplay(obj, articleId);
            }else{
                alert("空内容");
            }
        });
    }


    $.fn.addCommentList=function(options){
        var defaults = {
            data:[],
            add:""
        }
        var option = $.extend(defaults, options);

        //加载数据
        if(option.data.length > 0){
            var dataList = option.data;
            var totalString = "";
            for(var i=0;i<dataList.length;i++){
                var obj = dataList[i];
                var objString = crateCommentInfo(obj);
                totalString = totalString+objString;
            }
            $(this).append(totalString).find(".reply-btn").click(function(){
                if($(this).parent().parent().find(".replybox").length > 0){
                    $(".replybox").remove();
                }else{
                    $(".replybox").remove();
                    replyClick($(this));
                }
            });
            $(".reply-list-btn").click(function(){
                if($(this).parent().parent().find(".replybox").length > 0){
                    $(".replybox").remove();
                }else{
                    $(".replybox").remove();
                    replyClick($(this));
                }
            })
        }

        //添加新数据
        if(option.add != ""){
            obj = option.add;
            //console.info(obj);
            var str = crateCommentInfo(obj);
            $(this).prepend(str).find(".reply-btn").click(function(){
                replyClick($(this));
            });
        }
    }


})(jQuery);